package com.cc.druidtrans.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
 * @author chanchaw
 * @create 2022-11-27 9:59
 */
public class DruidUtils {
    public static Connection getConnection() throws Exception {
        Properties prop = new Properties();
        prop.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
        System.out.println(prop.getProperty("driverClassName"));
        DataSource ds = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = ds.getConnection();
        return conn;
    }

    public static void closeAll(Connection conn, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
